package com.ladybug.flow.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ladybug.flow.modules.system.domain.entitys.SysResources;
import com.ladybug.flow.modules.system.domain.vo.MenuVO;
import com.ladybug.flow.modules.system.service.SysResourcesService;
import com.ladybug.flow.modules.system.mapper.SysResourcesMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author long
* @description 针对表【sys_resources(系统资源表)】的数据库操作Service实现
* @createDate 2022-10-17 14:13:01
*/
@Service
public class SysResourcesServiceImpl extends ServiceImpl<SysResourcesMapper, SysResources>
    implements SysResourcesService{

    /**
     * @return {@code MenuVO }
     * @description: 获取树形菜单
     * @Author: xianglongfei
     * @Date: 2023/01/11 08:52:11
     */
    @Override
    public List<MenuVO> getMenu() {
        List<MenuVO> menuVOList = this.baseMapper.getMenu();
        List<MenuVO> menuList = this.getMenuVO(menuVOList);
        return menuList;
    }

    @Override
    public List<SysResources> getMenuList(String pid) {
        List<SysResources> sysResourcesList = this.baseMapper.getFirstMenu(pid);
        return sysResourcesList;
    }

    /**
     * @return {@code List<SysResources> }
     * @description: 获取资源列表Tree（菜单类）
     * @Author: xianglongfei
     * @Date: 2023/01/12 11:23:12
     */
    @Override
    public List<SysResources> listTree() {
        List<SysResources> list = this.list(new LambdaQueryWrapper<SysResources>().eq(SysResources::getResourceType,"SORT").eq(SysResources::getDeleted,0).orderByAsc(SysResources::getResourceOrder));
        list = buildTree(list);
        return list;
    }

    /**
     * @param sysResources 系统资源
     * @description: 修改
     * @Author: xianglongfei
     * @Date: 2023/01/13 08:37:13
     */
    @Override
    public void updateResource(SysResources sysResources) {
        String oldResourcePid = sysResources.getOldResourcePid();
        String resourcePid = sysResources.getResourcePid();
        this.updateById(sysResources);
        if(oldResourcePid != null && !"".equals(oldResourcePid) && !oldResourcePid.equals(resourcePid)){
            // 更新oldResourcePid所对应的资源是否有子集
            long count = this.count(new LambdaQueryWrapper<SysResources>().eq(SysResources::getResourcePid,oldResourcePid));
            if(count == 0){
                this.update(new LambdaUpdateWrapper<SysResources>().set(SysResources::getHasChildren,0).eq(SysResources::getId,oldResourcePid));
            }
        }
        if(resourcePid != null && !"".equals(resourcePid) && !oldResourcePid.equals(resourcePid)){
            //更新resourcePid所对应资源是否有子集
            this.update(new LambdaUpdateWrapper<SysResources>().set(SysResources::getHasChildren,1).eq(SysResources::getId,resourcePid));
        }
    }

    @Override
    public void addResource(SysResources sysResources) {
        String resourcePid = sysResources.getResourcePid();
        this.save(sysResources);
        if(resourcePid != null && !"".equals(resourcePid) ){
            //更新resourcePid所对应资源是否有子集
            this.update(new LambdaUpdateWrapper<SysResources>().set(SysResources::getHasChildren,1).eq(SysResources::getId,resourcePid));
        }
    }

    private List<SysResources> buildTree(List<SysResources> list) {
        List<SysResources> menuTree = new ArrayList<>();
        // 1、获取第一级节点
        for (SysResources sysResources : list) {
            if("".equals(sysResources.getResourcePid())) {
                menuTree.add(sysResources);
            }
        }
        // 2、递归获取子节点
        for (SysResources sysResources : menuTree) {
            sysResources = getChildrenResource(sysResources, list);
        }
        return menuTree;
    }

    private SysResources getChildrenResource(SysResources psysResources, List<SysResources> list) {
        List<SysResources> childMenus = new ArrayList<>();
        for (SysResources sysResources : list) {
            if(Objects.equals(psysResources.getId(),sysResources.getResourcePid())) {
                sysResources = getChildrenResource(sysResources, list);
                childMenus.add(sysResources);
            }
        }
        if(childMenus.size()>0){
            psysResources.setChildren(childMenus);
        }
        return psysResources;
    }

    /**
     * @param menuVOList 系统资源列表
     * @return {@code List<SysResources> }
     * @description: 组装树形菜单
     * @Author: xianglongfei
     * @Date: 2023/01/11 09:10:11
     */
    private List<MenuVO> getMenuVO(List<MenuVO> menuVOList) {
        List<MenuVO> menuTree = new ArrayList<>();
        // 1、获取第一级节点
        for (MenuVO menuVO : menuVOList) {
            if("".equals(menuVO.getPid())) {
                menuTree.add(menuVO);
            }
        }
        // 2、递归获取子节点
        for (MenuVO menuVO : menuTree) {
            menuVO = getChildrenMenuVO(menuVO, menuVOList);
        }
        return menuTree;
    }

    /**
     * @param pmenuVO    小组资源
     * @param menuVOList 系统资源列表
     * @return {@code SysResources }
     * @description: 递归
     * @Author: xianglongfei
     * @Date: 2023/01/11 09:10:11
     */
    private MenuVO getChildrenMenuVO(MenuVO pmenuVO, List<MenuVO> menuVOList) {
        List<MenuVO> childMenus = new ArrayList<>();
        for (MenuVO menuVO : menuVOList) {
            if(Objects.equals(pmenuVO.getId(),menuVO.getPid())) {
                menuVO = getChildrenMenuVO(menuVO, menuVOList);
                childMenus.add(menuVO);
            }
        }
        if(childMenus.size()>0){
            pmenuVO.setChildren(childMenus);
        }
        return pmenuVO;
    }
}




